<template>
  <div class="box app-container">
    <el-row>
      <el-col :span="24">
        <el-card shadow="never" :body-style="{height:'calc(100vh - 230px)'}">
          <el-col :span="24">
            <el-button type="primary" @click="addItem" icon="el-icon-plus"
                       size="medium"
            >添 加
            </el-button>
          </el-col>
          <el-col :span="24" style="margin-top: 20px;">
            <el-table
              :data="tableData"
              style="width: 100%;min-height: 440px;"
              row-key="path"
              border
              default-expand-all
              :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
            >
              <el-table-column
                label="标题"
                width="170"
                show-overflow-tooltip
              >
                <template slot-scope="scope">{{ scope.row.meta.title }}</template>
              </el-table-column>
              <el-table-column
                prop="name"
                width="100"
                show-overflow-tooltip
                label="name"
              >
              </el-table-column>
              <el-table-column
                prop="path"
                label="路径"
                width="120"
                show-overflow-tooltip
              >
              </el-table-column>
              <el-table-column
                label="是否隐藏"
              >
                <template slot-scope="scope">{{ scope.row.hidden ? '是' : '否' }}</template>
              </el-table-column>
              <el-table-column
                prop="hidden"
                label="是否有子节点"
              >
                <template slot-scope="scope">{{ scope.row.children ? '是' : '否' }}</template>
              </el-table-column>
              <el-table-column
                prop="path"
                show-overflow-tooltip
                label="vue文件路径"
              >
              </el-table-column>
              <el-table-column
                label="重定向"
                width="80"
                show-overflow-tooltip
              >
                <template slot-scope="scope">{{ scope.row.redirect ? scope.row.redirect : '无' }}</template>
              </el-table-column>
              <el-table-column
                prop="hidden"
                width="80"
                label="图标"
              >
                <template slot-scope="scope">
                  <Item :icon="scope.row.meta && scope.row.meta.icon"></Item>
                </template>
              </el-table-column>
              <el-table-column label="操作" width="150">
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    @click="handleEdit(scope.row)"
                  >编辑
                  </el-button>
                  <el-button
                    size="mini"
                    type="danger"
                    @click="handleDelete(scope.$index, scope.row)"
                  >删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-col>
          <el-dialog
            title="提示"
            :visible.sync="dialogVisible"
            width="60%"
          >
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="140px" class="demo-form-inline"
                     :inline="true"
            >
              <el-form-item label="父级id" prop="id">
                <el-input v-model="ruleForm.id"></el-input>
              </el-form-item>
              <el-form-item label="name" prop="name">
                <el-input v-model="ruleForm.name"></el-input>
              </el-form-item>
              <el-form-item label="路径" prop="path">
                <el-input v-model="ruleForm.path"></el-input>
              </el-form-item>
              <el-form-item label="vue文件路径" prop="vueFile">
                <el-input v-model="ruleForm.vueFile"></el-input>
              </el-form-item>
              <el-form-item label="重定向" prop="redirect">
                <el-input v-model="ruleForm.redirect"></el-input>
              </el-form-item>
              <el-form-item label="标题" prop="title">
                <el-input v-model="ruleForm.title"></el-input>
              </el-form-item>
              <el-form-item label="图标" prop="icon">
                <el-input v-model="ruleForm.icon"></el-input>
              </el-form-item>
              <el-form-item label="badge" prop="badge">
                <el-input v-model="ruleForm.badge"></el-input>
              </el-form-item>
              <el-form-item label="dot" prop="dot">
                <el-switch v-model="ruleForm.dot"></el-switch>
              </el-form-item>
              <el-form-item label="隐藏" prop="hidden">
                <el-switch v-model="ruleForm.hidden"></el-switch>
              </el-form-item>
              <el-form-item label="始终显示当前节点" prop="showDot">
                <el-switch v-model="ruleForm.showDot"></el-switch>
              </el-form-item>
              <el-form-item label="自定义svg图标" prop="svgIcon">
                <el-switch v-model="ruleForm.svgIcon"></el-switch>
              </el-form-item>
              <el-form-item label="固定" prop="fixed">
                <el-switch v-model="ruleForm.fixed"></el-switch>
              </el-form-item>
              <el-form-item label="无缓存" prop="cache">
                <el-switch v-model="ruleForm.cache"></el-switch>
              </el-form-item>
              <el-form-item label="不显示当前标签页" prop="showLabel">
                <el-switch v-model="ruleForm.showLabel"></el-switch>
              </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
              <el-button @click="dialogVisible = false">取 消</el-button>
              <el-button type="primary" @click="onSubmit('ruleForm')">确 定</el-button>
            </span>
          </el-dialog>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import Item from '@/layout/components/Sidebar/Item'

export default {
  name: 'index',
  components: { Item },
  data() {
    return {
      dialogVisible: false,
      tableData: [],
      ruleForm: {
        id: '',
        path: '',
        vueFile: '',
        redirect: '',
        title: '',
        icon: '',
        badge: '',
        name: '',
        dot: false,
        hidden: false,
        svgIcon: false,
        showDot: false,
        fixed: false,
        cache: false,
        showLabel: false
      },
      rules: {
        name: [
          { required: true, message: '请输入name', trigger: 'blur' }
        ],
        id: [
          { required: true, message: '请输入父级id', trigger: 'blur' }
        ],
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' }
        ],
        vueFile: [
          { required: true, message: '请输入component', trigger: 'blur' }
        ],
        path: [
          { required: true, message: '请输入路径', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    addItem() {
      this.dialogVisible = true
      this.resetForm('ruleForm')
    },
    resetForm(formName) {
      this.$nextTick(()=>{
        this.$refs[formName].resetFields();
      })
    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$message({
            message: '模拟保存成功',
            type: 'success'
          })
          this.dialogVisible = false
          this.ruleForm = Object.assign({}, )
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleEdit(row) {
      this.dialogVisible = true
      this.resetForm('ruleForm')
      this.$nextTick(()=>{
        this.ruleForm.title = row.meta.title
        this.ruleForm.name = row.name
        this.ruleForm.path = row.path
        this.ruleForm.vueFile = row.path
        this.ruleForm.icon = row.meta.icon
        this.ruleForm.redirect = row.redirect
      })
    },
    handleDelete() {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '模拟删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    fetchData() {
      let list = this.$router.options.routes.slice(3)
      list.pop()
      this.tableData = list
      this.dataList = list
      this.total = list.length
    }
  },
  mounted() {
    this.fetchData()
  }
}
</script>

<style scoped lang="scss">
.box {
  margin-bottom: 50px;

  .demo-form-inline {
    .el-form-item {
      margin-right: 32px;
    }

    ::v-deep.el-input {
      width: 200px;
    }
  }

  ::v-deep.el-form-item__label {
    font-weight: 400;
  }

  ::v-deep .has-gutter {
    tr {
      th {
        background-color: #F5F7FA;
      }
    }
  }

  ::v-deep.cell {
    font-size: 14px;
    font-weight: 400;
    text-align: center;
  }
}
</style>
